package com.tencent.wxcloudrun.service.impl;

import com.tencent.wxcloudrun.dao.UserMapper;
import com.tencent.wxcloudrun.model.User;
import com.tencent.wxcloudrun.service.UserService;
import com.tencent.wxcloudrun.utils.UidUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @author zhangshuai
 * @description
 * @create 2023/12/11 10:03
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getByName(String name) {
        User user = userMapper.getUserByName(name);
        return user;
    }

    @Override
    public String getPwByName(String name) {
        User user = userMapper.getUserByName(name);
        if (ObjectUtils.isEmpty(user)){
            return null;
        }
        return user.getPassword();
    }

    @Override
    public String checkUserInfo(User user) {
        String name = user.getName();
        String password = user.getPassword();
        if (ObjectUtils.isEmpty(name)||ObjectUtils.isEmpty(password)){
            return "名称或密码为空";
        }
        String nameTrim = name.trim();
        User dbName = getByName(nameTrim);
        if (!ObjectUtils.isEmpty(dbName)){
            return "用户名已存在";
        }
        // 不少于八位
        String pwTrim = password.trim();
        String math = ".{8,}";
        if (!pwTrim.matches(math)){
            return "密码不能少于八位（除空格）";
        }
        return null;
    }

    @Override
    public void saveUser(User user) {
        Integer uid = UidUtils.getUID();
        user.setId(uid);
        userMapper.saveUser(user.getId(),user.getName(),user.getPassword());
    }
}
